package ui;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import application.ApplicationModel;

import com.vaadin.annotations.Theme;
import com.vaadin.server.ExternalResource;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Accordion;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

import data.Celebrity;
import data.womenclothing.WomenClothing;
import data.womenclothing.apparel.Apparel;
import data.womenclothing.apparel.trouser.Jeans;
import data.womenclothing.apparel.trouser.Trouser;
import data.womenclothing.shoes.Shoes;


@SuppressWarnings("serial")
@Theme("demoopenis")
public class CelebrityClothesView extends VerticalLayout {

	private Celebrity celebrity;
	private ApplicationModel application;

	private List<WomenClothing> clothes;

	private HorizontalLayout celebrityLayout = new HorizontalLayout();


	public CelebrityClothesView(ApplicationModel application, Celebrity celebrity) {
		this.application = application;
		this.celebrity = celebrity;

		setMargin(new MarginInfo(false, true, false, true));
		setSpacing(true);

		initCelebrityLayout();

		// <loading>
		clothes = this.application.findCelebrityClothes(celebrity);
		// </loading>
		initClothesLayout();
	}


	private void initCelebrityLayout() {
		addComponent(celebrityLayout);
		celebrityLayout.setWidth("100%");
		celebrityLayout.setSpacing(true);

		Label text = new Label("<h2>" + celebrity.getStageName() + "</h2><hr>"
				+ "<p>" + celebrity.getDescription() + "</p>", ContentMode.HTML);
		celebrityLayout.addComponent(text);

		celebrityLayout.setExpandRatio(text, 3);

		if(celebrity.hasImage()) {
			Image image = new Image("", new ExternalResource(celebrity.getImage()));
			celebrityLayout.addComponent(image);
			celebrityLayout.setComponentAlignment(image, Alignment.TOP_RIGHT);
			celebrityLayout.setExpandRatio(image, 1);
		}
	}

	private void initClothesLayout() {
		VerticalLayout layout = new VerticalLayout();
		addComponent(layout);

		Label text = new Label(
				"<h2>Clothes worn by " + celebrity.getStageName() + ":</h2>", 
				ContentMode.HTML
				);
		layout.addComponent(text);

		Accordion accordion = new Accordion();
		for(WomenClothing clothing : clothes)
			accordion.addTab(
					getClothingOverview(clothing), 
					getClothingTitle(clothing)
					);

		accordion.setSizeFull();

		layout.addComponent(accordion);
	}

	private String getClothingTitle(WomenClothing clothing) {
		return clothing.getProductDescription();
	}

	private Component getClothingOverview(WomenClothing clothing) {
		VerticalLayout layout = new VerticalLayout();
		layout.setMargin(true);
		
		layout.addComponent(getGeneralOverview(clothing));
		layout.addComponent(getSubtypeOverview(clothing));

		return layout;
	}

	private Component getGeneralOverview(WomenClothing clothing) {
		VerticalLayout layout = new VerticalLayout();

		layout.addComponent(new Label("<b>Type</b>: " + StringUtils.join(clothing.getSubtypes(), ": "), ContentMode.HTML));
		layout.addComponent(new Label("<b>Brand</b>: " + clothing.getBrand(), ContentMode.HTML));
		layout.addComponent(new Label("<b>Colors</b>: " + StringUtils.join(clothing.getColors(), ", "), ContentMode.HTML));
		layout.addComponent(new Label("<b>Materials</b>: " + StringUtils.join(clothing.getMaterials(), ", "), ContentMode.HTML));	

		return layout;
	}

	private Component getSubtypeOverview(WomenClothing clothing) {
		VerticalLayout layout = new VerticalLayout();
		for(String type : clothing.getSubtypes())
			layout.addComponent(dispatchSubtypeView(type, clothing));

		return layout;
	}

	private Component dispatchSubtypeView(String type, WomenClothing clothing) {
		if(type.equals(Apparel.TYPE))
			return new Label(
					"<b>Size(s)</b>: " 
							+ StringUtils.join(((Apparel) clothing).getSizes(), ", "), 
					ContentMode.HTML
					);

		if(type.equals(Trouser.TYPE))
			return new Label(
					"<b>Fastenings</b>: " 
							+ StringUtils.join(((Trouser) clothing).getFastenings(), ", "), 
					ContentMode.HTML
					);

		if(type.equals(Jeans.TYPE)) {
			if(((Jeans) clothing).hasLegCut())
				return new Label(
						"<b>Leg cut</b>: " 
								+ ((Jeans) clothing).getLegCut(),
						ContentMode.HTML
						);
			else return new Label();
		}

		if(type.equals(Shoes.TYPE)) {
			VerticalLayout layout = new VerticalLayout();
			layout.addComponent(new Label(
					"<b>Size</b>: "
							+ ((Shoes) clothing).getSize(), 
					ContentMode.HTML
					));
			if(((Shoes) clothing).hasHeelHeight())
				layout.addComponent(new Label(
						"<b>Heel height</b>: " 
								+ ((Shoes) clothing).getHeelHeight(),
						ContentMode.HTML
						));
			return layout;
		}

		return new Label();
	}


}
